var prefix = jsctx + "/tenant/menu";

var tenantMenu = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        },10)
    };

    function load() {
        $('#exampleTable')
            .bootstrapTreeTable(
                {
                    id: 'tenantMenuId',
                    code: 'tenantMenuId',
                    parentCode: 'parentId',
                    type: "GET",
                    url: prefix + '/list?userType=' + $("#userType").val(),
                    ajaxParams: {sort: 'sort'},
                    expandColumn: '1',
                    striped: true,
                    bordered: true,
                    expandAll: false,
                    // toolbar : '#exampleToolbar',
                    columns: [
                        {title: '编号', field: 'tenantMenuId', align: 'center'},
                        {title: '名称', field: 'menuName', align: 'left'},
                        {title: '显示', field: 'status', align: 'center'},
                        {title: '排序', field: 'sort', align: 'center'},
                        {
                            title: '图标', field: 'icon', align: 'center',
                            formatter: function (item, index) {
                                return item.icon == null ? ''
                                    : '<i class="' + item.icon + '"></i>';
                            }
                        },
                        {
                            title: '类型',
                            field: 'menuType',
                            align: 'center',
                            valign: 'center',
                            formatter: function (item, index) {
                                if (item.menuType === '0') {
                                    return '<span class="label label-primary">目录</span>';
                                } else if (item.menuType === '1') {
                                    return '<span class="label label-success">菜单</span>';
                                } else if (item.menuType === '2') {
                                    return '<span class="label label-warning">按钮</span>';
                                }
                            }
                        },
                        {
                            title: '角色', field: 'userType', align: 'center', valign: 'center',
                            formatter: function (item, index) {
                                if (item.userType === '0') {
                                    return '<span class="label label-primary">运营商</span>';
                                } else if (item.userType === '1') {
                                    return '<span class="label label-primary">分销商</span>';
                                } else if (item.userType === '2') {
                                    return '<span class="label label-primary">企业帐户</span>';
                                }
                            }
                        },
                        {
                            title: '产品类型', field: 'serviceTypeCode', align: 'center', valign: 'center',
                            formatter: function (item, index) {
                                if (item.serviceTypeCode === 'STANDARD') {
                                    return '<span class="label label-success">标准</span>';
                                } else if (item.serviceTypeCode === 'SMS') {
                                    return '<span class="label label-success">短信服务</span>';
                                } else if (item.serviceTypeCode === 'CARD') {
                                    return '<span class="label label-success">刷卡充电</span>';
                                } else if (item.serviceTypeCode === 'TTS') {
                                    return '<span class="label label-success">语音服务</span>';
                                } else if (item.serviceTypeCode === 'E_INVOICE') {
                                    return '<span class="label label-success">电子发票</span>';
                                } else if (item.serviceTypeCode === 'HLHT') {
                                    return '<span class="label label-success">互联互通</span>';
                                } else if (item.serviceTypeCode === 'O2O') {
                                    return '<span class="label label-success">O2O周边服务</span>';
                                } else if (item.serviceTypeCode === 'PRINTER') {
                                    return '<span class="label label-success">出场小票打印机</span>';
                                } else if (item.serviceTypeCode === 'AUTO_GATE') {
                                    return '<span class="label label-success">自动化道闸对接</span>';
                                }
                            }
                        },
                        {title: 'URL', field: 'href', align: 'left'},
                        {title: '权限码', field: 'permission', align: 'left'},
                        {
                            title: '操作',width:'200',
                            field: 'tenantMenuId',
                            align: 'center',
                            valign: 'center',
                            formatter: function (item, index) {
                                var e = '<a class="btn btn-primary btn-sm '
                                    + has_edit
                                    + '" href="#" title="编辑" onclick="tenantMenu.edit(\''
                                    + item.tenantMenuId
                                    + '\')">编辑</a> ';

                                var p = '<a class="btn btn-primary btn-sm '
                                    + has_add
                                    + '" href="#" title="增加下级" onclick="tenantMenu.add(\''
                                    + item.tenantMenuId
                                    + '\')">增加下级</a> ';

                                // if (item.menuType === '0') {
                                //     var p = '<a class="btn btn-primary btn-sm '
                                //         + has_add
                                //         + '" href="#" title="增加下级" onclick="tenantMenu.add(\''
                                //         + item.tenantMenuId
                                //         + '\')">增加下级</a> ';
                                // } else if (item.menuType === '1') {
                                //     var p = `<a class="btn btn-primary btn-sm `
                                //         + has_add
                                //         + `" href="#" title="功能授权" onclick="tenantMenu.genPermission(`
                                //         + item.tenantMenuId
                                //         + `)">功能授权</a> `;
                                // } else {
                                //     var p = '';
                                // }

                                var d = '<a class="btn btn-warning btn-sm ' + has_delete + '" href="#" title="删除" onclick="tenantMenu.remove(\''
                                    + item.tenantMenuId + '\',' + '\'' + item.menuName
                                    + '\')">删除</a> ';
                                return e + p + d;
                            }
                        }]
                });
    };

    function reLoad() {
        load();
    }

    function add(parentId) {
        location.href = prefix + "/add/" + parentId;
    }

    function edit(id) {
        location.href = prefix + "/edit/" + id;
    }

    function remove(id, name) {
        layer.confirm('下级菜单及授权信息将同时被删除，确定要删除选中的记录？' + name, {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/remove",
                type: "post",
                data: {
                    'id': id
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("删除成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    function genPermission(tenantMenuId) {
        layer.open({
            type: 2,
            title: '功能授权',
            shadeClose: false,
            area: ['800px', '550px'],
            content: prefix + '/genPermission/' + tenantMenuId
        });
    }

    function goBack() {
        location.href = prefix + "/index";
    }

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        add: add,
        edit: edit,
        remove: remove,
        genPermission: genPermission
    };
})();

var addTenantMenu = (function () {
    function init() {
        validateRule();

        //打开图标列表
        $("#ico-btn").click(function () {
            layer.open({
                type: 2,
                title: '图标列表',
                content: jsctx + '/include/fontIcoList.html',
                area: ['480px', '90%'],
                success: function (layero, index) {
                    //var body = layer.getChildFrame('.ico-list', index);
                    //console.log(layero, index);
                }
            });
        });
    };

    // $.validator.setDefaults({
    //     submitHandler: function () {
    //         save();
    //     }
    // });

    function save() {
        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/save",
            data: $('#frmMenu').serialize(),
            async: false,
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                if (data.status == 200) {
                    parent.layer.msg("保存成功");
                    tenantMenu.goBack();
                } else {
                    parent.layer.alert(data.message)
                }
            }
        });
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmMenu").validate({
            rules: {
                menuName: {
                    required: true
                },
                menuType: {
                    required: true
                },
                userType: {
                    required: true
                },
                serviceTypeCode: {
                    required: true
                }
            },
            messages: {
                menuName: {
                    required: icon + "请输入菜单名"
                },
                menuType: {
                    required: icon + "请选择菜单类型"
                },
                userType: {
                    required: icon + "请选择只适合用户类型"
                },
                serviceTypeCode: {
                    required: icon + "请选择产品类型"
                }
            },
            submitHandler: function (form) {
                save();
            }
        })
    }

    return {
        init: init
    };
})();

var editTenantMenu = (function () {
    function init() {
        validateRule();

        //打开图标列表
        $("#ico-btn").click(function () {
            layer.open({
                type: 2,
                title: '图标列表',
                content: jsctx + '/include/fontIcoList.html',
                area: ['480px', '90%'],
                success: function (layero, index) {
                    //var body = layer.getChildFrame('.ico-list', index);
                    //console.log(layero, index);
                }
            });
        });
    };

    function update() {
        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/save",
            data: $('#frmMenu').serialize(),// 你的formid
            async: false,
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                if (data.status == 200) {
                    parent.layer.msg("保存成功");
                    tenantMenu.goBack();
                } else {
                    parent.layer.alert(data.message)
                }

            }
        });
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmMenu").validate({
            rules: {
                menuName: {
                    required: true
                },
                menuType: {
                    required: true
                },
                userType: {
                    required: true
                },
                serviceTypeCode: {
                    required: true
                }
            },
            messages: {
                menuName: {
                    required: icon + "请输入菜单名"
                },
                menuType: {
                    required: icon + "请选择菜单类型"
                },
                userType: {
                    required: icon + "请选择只适合用户类型"
                },
                serviceTypeCode: {
                    required: icon + "请选择产品类型"
                }
            },
            submitHandler: function (form) {
                update();
            }
        })
    }

    return {
        init: init
    };
})();